from datetime import datetime

from django.db import models
from django.utils import timezone

from user.models import BaseModel, User
from zhineng.models import College


class QuestionType(models.Model):
    name = models.CharField(max_length=100)

    class Meta:
        db_table = 'question_type'


class TestQuestions(BaseModel):
    title = models.CharField(max_length=100)
    options = models.CharField(max_length=100, null=True)
    answer = models.TextField()
    question_type = models.ForeignKey(QuestionType, on_delete=models.CASCADE, related_name='question_type')
    add_time = models.DateField(auto_now_add=True)

    class Meta:
        db_table = 'test_questions'

    def to_json(self):
        return {
            'title': self.title,
            'options': self.options,
            'answer': self.answer,
            'question_type': self.question_type.id,
        }


class ExaminationPaper(BaseModel):
    title = models.CharField(max_length=100)
    create_time = models.DateField(default=timezone.now)
    questions_info = models.TextField()
    answer = models.TextField(default='')
    college = models.ForeignKey(College, on_delete=models.CASCADE, default='')
    start_time = models.DateTimeField(default=timezone.now)
    end_time = models.DateTimeField(default=timezone.now)

    class Meta:
        db_table = 'examination_paper'


class TestResult(BaseModel):
    answer_user = models.ForeignKey(User, related_name='answer_user', on_delete=models.CASCADE)
    examination_paper = models.ForeignKey(ExaminationPaper, related_name='examination_paper', on_delete=models.CASCADE)
    answer_info = models.TextField()
    score = models.TextField()
    is_corrected = models.BooleanField(default=False)
    create_time = models.DateField(default=timezone.now)
    file_id = models.TextField(null=True)

    class Meta:
        db_table = 'test_result'

    def to_json(self):
        return {
            'answer_user': self.answer_user.username,
            'examination_paper': self.examination_paper.title,
            'answer_info': self.answer_info,
            'score': self.score,
        }


class Score(BaseModel):
    """成绩表"""
    exam_score = models.FloatField(verbose_name="考试分数")
    composition_score = models.FloatField(verbose_name="综合积分")
    study_degree = models.IntegerField(verbose_name="学度")
    test = models.ForeignKey(TestResult, related_name="test", on_delete=models.CASCADE, verbose_name="考试")
    user = models.ForeignKey(User, related_name="user", on_delete=models.CASCADE, verbose_name="考生")

    class Meta:
        db_table = "score"

    def to_json(self):
        return {
            'exam_score': self.exam_score,
            'composition_score': self.composition_score,
            'study_degree': self.study_degree,
            'test': self.test,
            'user': self.user
        }


class ExaminationResult(BaseModel):
    score = models.DecimalField(max_digits=4, decimal_places=1)
    stu_id = models.IntegerField(default=0)
    exam_date = models.DateField(default='')
    classes = models.CharField(max_length=10)

    class Meta:
        db_table = 'examination_result'


